package com.jtjrenren.android.taxi.passenger.core.user.details;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.bean.api.Result;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by wdl on 2016/1/22.
 */
public class UserDetailActivity extends BaseActivity {
    @BindView(R.id.user_details_phone)
    TextView tv_phone;
    @BindView(R.id.user_details_sex_desc)
    TextView tv_sex;
    @BindView(R.id.user_details_sex)
    LinearLayout ll_sex;
    @BindView(R.id.user_details_name)
    EditText et_name;
    @BindView(R.id.user_details_age)
    EditText et_age;
    @BindView(R.id.user_details_addrdetails)
    EditText et_addr;

    private User user;
    private PopupWindow showSexPopWin;
    private TextView popTvSex;

    private boolean isChanged = false;

    @Override
    public int getLayoutId() {
        return R.layout.aty_user_details;
    }

    @Override
    public void init() {
        user = AppManger.getInstance().user;

        ((TextView) findViewById(R.id.title)).setText("个人资料");

        initUserViews(user);
    }


    private void initUserViews(User user) {
        if (user == null)
            return;

        tv_phone.setText(user.getPhone() == null ? "" : user.getPhone());
        et_name.setText(user.getName() == null ? "" : user.getName());
        et_addr.setText(user.getPostAddress() == null ? "" : user.getPostAddress());
        et_age.setText(user.getAge() == null ? "" : user.getAge());
        if (user.getSex() != null) {
            tv_sex.setText("0".equals(user.getSex()) ? getString(R.string.woman) : getString(R.string.man));
        }
    }

    @OnTextChanged(R.id.user_details_age)
    void onTextChangedAget(CharSequence text) {
        isChanged = true;
    }


    @OnTextChanged(R.id.user_details_addrdetails)
    void onTextChangedAddr(CharSequence text) {
        isChanged = true;
    }

    @OnTextChanged(R.id.user_details_name)
    void onTextChangedName(CharSequence text) {
        isChanged = true;
    }


    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.user_details_commit)
    public void clickCommit() {
        if (!isChanged) {
            displayShort("未做任何修改");
            return;
        }
        String age = et_age.getText().toString().trim();
        String name = et_name.getText().toString().trim();
        String address = et_addr.getText().toString().trim();

        final String rname = user.getName();
        final String rage = user.getAge();
        final String raddr = user.getPostAddress();

        user.setAge(age);
        user.setName(name);
        user.setPostAddress(address);
        ApiWorks.updateUserInfo(user, new ApiWorks.ResponseListener<Result>() {
            @Override
            public void onResponse(Result result) {
                if (result != null && result.getResult().equalsIgnoreCase("us0006")) {
                    displayShort("更新成功");
                    finish();
                } else {
                    displayShort("更新失败");
                    if (user == null)
                        return;
                    user.setName(rname);
                    user.setAge(rage);
                    user.setPostAddress(raddr);
                }
            }
        });
    }

    @OnClick(R.id.user_details_sex)
    public void clickSex() {
        showSexPopWin();
    }

    private void showSexPopWin() {
        if (showSexPopWin == null) {
            popTvSex = new TextView(this);
            popTvSex.setPadding(10, 10, 10, 10);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popTvSex.setGravity(Gravity.CENTER);
            popTvSex.setLayoutParams(params);
            popTvSex.setTextSize(16);

            popTvSex.setClickable(true);
            popTvSex.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (showSexPopWin != null && showSexPopWin.isShowing())
                        showSexPopWin.dismiss();
                    String sex = popTvSex.getText().toString().trim();
                    tv_sex.setText(sex);
                    isChanged = true;
                    if (user != null)
                        user.setSex(getString(R.string.woman).equals(sex) ? "0" : "1");
                }
            });

            showSexPopWin = new PopupWindow(popTvSex, ll_sex.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
            showSexPopWin.setOutsideTouchable(true);
            showSexPopWin.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_btn_gray));

            showSexPopWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {

                }
            });
        }
        popTvSex.setText(getString(R.string.woman).equals(tv_sex.getText().toString().trim()) ? getString(R.string.man) : getString(R.string.woman));
        showSexPopWin.showAsDropDown(ll_sex);
    }
}
